﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class manager_systemSetting_moduleManager : Page
{
    DBHelper db = new DBHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindData();
        }
    }

    private void bindData()
    {
        string sql = "select a.* from";
        sql += " (";
        sql += " SELECT ID, moduleName, url, icon, parentID, isUsing, parentName,[index], id as pxid, 'R' as sign FROM v_Module  where parentID = 0";
        sql += " union";
        sql += " SELECT ID, moduleName, url, icon, parentID, isUsing, parentName,[index], parentID as pxid, 'B' as sign FROM v_Module  where ";
        sql += " parentID > 0) a order by a.pxid,sign desc,a.[index]";
        DataTable dt = db.getList(sql);
        rptmoduleManager.DataSource = dt;
        rptmoduleManager.DataBind();

        sql = "select * from Module where parentid=0 order by [index]";
        dt = db.getList(sql);
        ddlParent.DataSource = dt;
        ddlParent.DataTextField = "moduleName";
        ddlParent.DataValueField = "ID";
        ddlParent.DataBind();
        ddlParent.Items.Insert(0, new ListItem("-无-", "0"));
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            string deleteSql = "DELETE [Module] WHERE ID=" + StringHelper.SafeSqlInput(hidBID.Value);
            int i = db.executeNoneQuery(deleteSql);
            if (i > 0)
            {
                deleteSql = "DELETE FROM [dbo].[RolePermissions] WHERE moduleID=" + StringHelper.SafeSqlInput(hidBID.Value);

                db.executeNoneQuery(deleteSql);

                CacheHelper.clearAllCache();
                Response.Write(CommonHelper.alertMsgAndRedirect("删除成功", Request.Url.LocalPath + Request.Url.Query));
            }
            else
            {
                Response.Write(CommonHelper.alertMsg("删除失败"));
            }
        }
    }

    protected void btnGet_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            DataTable dt = db.getList("1", "[Module]", "ID,moduleName,url,icon,parentID,isUsing,[index]", "ID=" + StringHelper.SafeSqlInput(hidBID.Value), "ID ASC");
            if (dt.Rows.Count > 0)
            {
                txtName.Text = dt.Rows[0]["moduleName"].ToString().Trim();
                txtUrl.Text = dt.Rows[0]["url"].ToString().Trim();
                txtIcon.Text = dt.Rows[0]["icon"].ToString().Trim();
                txtIndex.Text = dt.Rows[0]["index"].ToString().Trim();
                try
                {
                    ddlParent.SelectedValue = dt.Rows[0]["parentID"].ToString().Trim();
                }
                catch
                {
                    ddlParent.SelectedIndex = 0;
                }
                ddlIsUsing.SelectedValue = dt.Rows[0]["isUsing"].ToString().Trim() == "True" ? "1" : "0";
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string insertSql = "INSERT INTO [Module] (moduleName,url,icon,isusing,parentID,[index]) VALUES (N'" + StringHelper.SafeSqlInput(txtName.Text.Trim()) + "','" + txtUrl.Text.Trim() + "','" + StringHelper.SafeSqlInput(txtIcon.Text.Trim()) + "'," + StringHelper.SafeSqlInput(ddlIsUsing.SelectedValue) + "," + StringHelper.SafeSqlInput(ddlParent.SelectedValue) + "," + StringHelper.SafeSqlInput(txtIndex.Text.Trim()) + ")";

        int i = db.executeNoneQuery(insertSql);
        if (i > 0)
        {
            CacheHelper.clearAllCache();
            Response.Write(CommonHelper.alertMsgAndRedirect("添加成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("添加失败"));
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (hidBID.Value.Trim() != "")
        {
            string updateSql = "UPDATE [Module] SET moduleName=N'" + StringHelper.SafeSqlInput(txtName.Text.Trim()) + "',url='" + txtUrl.Text.Trim() + "',isusing=" + StringHelper.SafeSqlInput(ddlIsUsing.SelectedValue) + ",icon='" + StringHelper.SafeSqlInput(txtIcon.Text.Trim()) + "',parentID=" + StringHelper.SafeSqlInput(ddlParent.SelectedValue) + ",[index]=" + StringHelper.SafeSqlInput(txtIndex.Text) + " WHERE ID=" + StringHelper.SafeSqlInput(hidBID.Value);

            int i = db.executeNoneQuery(updateSql);
            if (i > 0)
            {
                CacheHelper.clearAllCache();
                Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
            }
            else
            {
                Response.Write(CommonHelper.alertMsg("修改失败"));
            }
        }
    }
}